/*
 * 
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.page;

import com.cleangwt.bootstrap.client.style.Btn;
import com.cleangwt.bootstrap.client.ui.Alert;
import com.cleangwt.bootstrap.client.ui.GroupFormPanel;
import com.cleangwt.bootstrap.support.client.TooltipErrorHandler;
import com.cleangwt.gadget.client.phone.PhoneField.PhoneType;
import com.cleangwt.gadget.client.phone.PhoneGadget;
import com.cleangwt.page.client.PageContext.CodeSplit;
import com.cleangwt.page.client.PageContext.Page;
import com.cleangwt.page.client.PageContext.Prefetch;
import com.cleangwt.showcase.client.component.ShowcasePage;
import com.cleangwt.showcase.client.resource.PageGroups;
import com.cleangwt.showcase.client.resource.i18n.ShowcaseMessages;
import com.cleangwt.showcase.client.resource.i18n.ValidatorMessages;
import com.cleangwt.uibinder.client.RequiresFormRebind;
import com.cleangwt.uibinder.client.UiInput;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.TextBox;
import com.cleangwt.validation.client.DeferredValidation;
import com.cleangwt.validation.client.Validator;
import com.cleangwt.validation.client.constraints.NotNull;
import com.cleangwt.validation.client.constraints.Remote;
import com.cleangwt.validation.client.constraints.relative.Eq;
import com.cleangwt.web.client.ObjectCallback;
import com.cleangwt.web.client.Requests;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
@Prefetch(PageGroups.VALIDATION)
@CodeSplit
@Page
public abstract class RemoteValidationDemo extends ShowcasePage implements RequiresFormRebind {
  ValidatorMessages c = GWT.create(ValidatorMessages.class);

  @UiInput
  @NotNull
  @Remote(url = "validate/validate1.do")
  TextBox txtfRemote1 = new TextBox();

  @UiInput
  TextBox txtfAnotherRemote1 = new TextBox();

  @UiInput
  @Remote(url = "validate/validate2.do", message = "{c.letterOnly}")
  @Eq(field = "txtbGroup", name1 = "Remote2", name2 = "Field1")
  TextBox txtfRemote2 = new TextBox();

  @UiInput
  TextBox txtbGroup = new TextBox();

  public RemoteValidationDemo() {
    super(ShowcaseMessages.Impl.remotevalidation(), "Invoke remote service to validate.",
        "RemoteValidationDemo.java");

    PhoneGadget phone = new PhoneGadget(PhoneType.EXT);
    Button btnVerify = new Button(c.verify());
    final Validator validator = new Validator(new TooltipErrorHandler());

    GroupFormPanel layout = new GroupFormPanel();
    setContentWidget(layout);

    layout.buildVertical().add("Remote1", txtfRemote1).div(6).add("Another Remote1",
        txtfAnotherRemote1).div(6).add("Remote2", txtfRemote2).div(6).add("Field1", txtbGroup).div(
        6).add("Phone Gadget", phone).div(6);

    layout.add(Btn.primary(btnVerify));

    this.addConstraints(validator);
    // validator.activate();

    validator.build(txtfAnotherRemote1).add(new DeferredValidation() {

      @Override
      protected void validateDeffered(Object value) {
        Requests.postObject("validate/validate1.do", value, new ObjectCallback<String>(null) {

          @Override
          protected void onCallback(String result) {
            if (result != null && result.isEmpty()) {
              errorReporter.reportErrorMessage(c.digitOnly());
            } else {
              errorReporter.reportErrorMessage(result);
            }
          }
        });
      }
    });

    validator.addValidator(phone.getValidator());

    btnVerify.addClickHandler(new ClickHandler() {

      @Override
      public void onClick(ClickEvent event) {
        if (validator.validate()) {
          Alert.alert(c.valid());
        }
      }
    });

    validator.activate();
  }

}